<?php

/**
 * Created by PhpStorm
 * Author niuyongqiang@kungeek.com
 * Date 2022/4/20 15:09
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\DB;

class CityChinaModel extends BaseModel
{
    protected $table = 'info_city_china';
    public $timestamps = false;


    /**
     * @param array $where
     * @param string $whereInKey
     * @param array $whereInValue
     * @param string $field
     * @return array
     */
    public static function getData(array $where=[], string $whereInKey='', array $whereInValue=[], string $field = '*'): array
    {
        $query = self::query()
            ->selectRaw($field)
            ->where($where);

        if (!empty($whereInKey) && !empty($whereInValue)){
            $query = $query->whereIn($whereInKey, $whereInValue);
        }

        $result = $query->get();

        return $result ? $result->toArray() : [];
    }

    /**
     * 查询分页
     * @param array $where
     * @param int $limit
     * @param string $field
     * @param string $whereInKey
     * @param array $whereInValue
     * @return array
     */
    public static function getPage(array $where, int $limit, string $field='*', string $whereInKey = '', array $whereInValue = [], int $fitPets = 1) : array
    {
        $query = self::query()
            ->selectRaw($field)
            ->where($where);

        if (!empty($whereInKey) && !empty($whereInValue)){
            $query->whereIn($whereInKey, $whereInValue);
        }

        if (!empty($fitPets)){
            $query->whereRaw(DB::raw('FIND_IN_SET(' . $fitPets . ', fit_pets)'));
        }

        $list = $query
            ->orderBy('id', 'desc')
            ->paginate($limit);

        return $list ? $list->toArray() : [];

    }

    /**
     * with-一级分类
     * @return HasMany
     */
    public function cat_id_msg(): HasMany
    {
        return $this->hasMany(PetsCategoryModel::class, 'id', 'cat_id');
    }

    /**
     * with-二级分类
     * @return HasMany
     */
    public function cat_id_2_msg(): HasMany
    {
        return $this->hasMany(PetsCategoryModel::class, 'id', 'cat_id_2');
    }

    /**
     * 查询一条
     * @param array $where
     * @param string $whereInKey
     * @param array $whereInValue
     * @return array|null
     */
    public static function getSingle(array $where = [], string $whereInKey='', array $whereInValue=[]): ?array
    {
        $query = self::query()
            ->where($where);

        if (!empty($whereInKey) && !empty($whereInValue)){
            $query = $query->whereIn($whereInKey, $whereInValue);
        }

        $user = $query->first();
        return $user ? $user->toArray() : [];
    }

    /**
     * 单条的创建
     * @param array $data
     * @return int
     */
    public static function insertSingle(array $data = []): int
    {
        return self::query()->insertGetId($data);
    }

    /**
     * 单条的修改
     * @param array $where
     * @param array $data
     * @return int
     */
    public static function updateSingle(array $where, array $data) : int
    {
        if (empty($where) || empty($data))
            return 0;

        return self::query()->where($where)->update($data);

    }

    /**
     * 邀请语with
     * @return HasMany
     */
    public function invite_language(): HasMany
    {
        return $this->hasMany(RpaInviteLanguageModel::class, 'id', 'rpa_invite_language_id');
    }

    /**
     * 虚拟号with
     * @return HasMany
     */
    public function wechat_user(): HasMany
    {
        return $this->hasMany(SysWechatUserModel::class, 'wechat_userid', 'execute_wechat_userid');
    }

    /**
     * 自增
     * @param array $where
     * @param string $key
     * @param int $num
     * @param string $whereInKey
     * @param array $whereInValue
     * @return int
     */
    public static function addIncrement(array $where = [], string $key = '', int $num = 1, string $whereInKey='', array $whereInValue=[]):int
    {
        $query = self::query()
            ->where($where);

        if (!empty($whereInKey) && !empty($whereInValue)){
            $query = $query->whereIn($whereInKey, $whereInValue);
        }

        return $query->increment($key, $num);
    }


    /**
     * 查询
     * @param array $where
     * @param string $whereInKey
     * @param array $whereInValue
     * @return array|null
     */
    public static function getUserData(array $where = [], string $whereInKey='', array $whereInValue=[]): ?array
    {
        $query = self::query()
            ->where($where);

        if (!empty($whereInKey) && !empty($whereInValue)){
            $query = $query->whereIn($whereInKey, $whereInValue);
        }

        $user = $query
            ->get();
        return $user ? $user->toArray() : [];
    }
}
